/**
 * 家系图编辑器
 * 初始化家系图中所有元素。
 * 此类只加载一次
 *
 * @class PedigreeEditor
 * @constructor
 */
var PedigreeEditor = Class.create({
	initialize: function() {
		//this.DEBUG_MODE = true;
		window.editor = this;
        this._externalEndpointManager = new ExternalEndpointsManager();
            
		this._graphModel = DynamicPositionedGraph.makeEmpty(PedigreeEditor.attributes.layoutRelativePersonWidth, PedigreeEditor.attributes.layoutRelativeOtherWidth);
		this._workspace = new Workspace();
		//判断是否隐藏某些元素
		var hiddenFlag = parent.$("#hiddenFlag").val();
		if(hiddenFlag == "true"){
			jQuery("#editor-menu").css("display","none");
			jQuery("#viewriskreport_button").css("display","none");
			this.getWorkspace().resetPaper();
		}
		this._nodeMenu = this.generateNodeMenu();
		this._nodeGroupMenu = this.generateNodeGroupMenu();
		this._partnershipMenu = this.generatePartnershipMenu();
		this._nodetypeSelectionBubble = new NodetypeSelectionBubble(false);
		this._siblingSelectionBubble = new NodetypeSelectionBubble(true);
		this._okCancelDialogue = new OkCancelDialogue();
		this._disorderLegend = new DisorgerLegend();
		
		
		this._geneLegend = new GeneLegend();
		this._hpoLegend = new HPOLegend();

		this._view = new View();

		this._actionStack = new ActionStack();
		this._importSelector = new ImportSelector();
		this._exportSelector = new ExportSelector();
		this._saveLoadIndicator = new SaveLoadIndicator();
		this._versionUpdater = new VersionUpdater();
		this._saveLoadEngine = new SaveLoadEngine();
		this._probandData = new ProbandDataLoader();
		
		this._printDialog = new PrintDialog();
		
		
		this._probandData.load(this._saveLoadEngine.load.bind(this._saveLoadEngine));
		console.log(this._probandData);

		this._controller = new Controller();

		var undoButton = $('action-undo');
		undoButton && undoButton.on("click", function(event) {
			document.fire("pedigree:undo");
		});
		var redoButton = $('action-redo');
		redoButton && redoButton.on("click", function(event) {
			document.fire("pedigree:redo");
		});

		var autolayoutButton = $('action-layout');
		autolayoutButton && autolayoutButton.on("click", function(event) {
			document.fire("pedigree:autolayout");
		});
		var clearButton = $('action-clear');
		clearButton && clearButton.on("click", function(event) {
			document.fire("pedigree:graph:clear");
		});

		var saveButton = $('action-save');
		saveButton && saveButton.on("click", function(event) {
			editor.getView().unmarkAll();
			editor.getSaveLoadEngine().save();
		});
		var loadButton = $('action-reload');
		loadButton && loadButton.on("click", function(event) {
			editor.getSaveLoadEngine().load();
		});

		var templatesButton = $('action-templates');
		templatesButton && templatesButton.on("click", function(event) {
			editor.getTemplateSelector().show();
		});
		var importButton = $('action-import');
		importButton && importButton.on("click", function(event) {
			editor.getImportSelector().show();
		});
		var exportButton = $('action-export');
		exportButton && exportButton.on("click", function(event) {
			editor.getExportSelector().show();
		});

		var closeButton = $('action-close');
		closeButton && closeButton.on("click", function(event) {
			window.location = XWiki.currentDocument.getURL('edit');
		});

		var renumberButton = $('action-number');
		renumberButton && renumberButton.on("click", function(event) {
			document.fire("pedigree:renumber");
		});

		var unsupportedBrowserButton = $('action-readonlymessage');
		unsupportedBrowserButton && unsupportedBrowserButton.on("click", function(event) {
			alert("浏览器不支持 ");
		});
		var viewLegendButton = $('action-viewLegend');
		viewLegendButton && viewLegendButton.on("click", function(event) {
			window.open('/static/family.pdf', '_blank');
		});	

		var exportImg = $('action-exportImg');
		exportImg && exportImg.on("click", function(event) {
			var svgEl = $$("#canvas svg")[0];
			var _bbox = $('canvas').down().getBBox();
			var canversWidth = _bbox.width;
			var canversHeight = _bbox.height;
			svgEl.insertBefore(new Element('rect', {"id": "white-bbox-background",
										"width": _bbox.width,
										"height": _bbox.height,
										"x": _bbox.x,
										"y": _bbox.y,
										"fill": "white"}),
					svgEl.firstChild
            );

			var svg = editor.getWorkspace().getSVGCopy();
			var svgXml = svg.getSVGText();
			var image = new Image();
			image.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgXml))); //给图片对象写入base64编码的svg流	
			
			var canvas = document.createElement('canvas');  //准备空画布
			canvas.width = canversWidth;
			canvas.height = canversHeight+100;

			var context = canvas.getContext('2d');  //取得画布的2d绘图上下文
			image.onload=function(){
				document.body.appendChild(image);
				var imgWidth = image.width;
				var imageHeight = image.height;
				context.drawImage(image, 0, 0,imgWidth,imageHeight);
				document.body.appendChild(image);
				var a = document.createElement('a');
				a.href = canvas.toDataURL('image/png');  //将画布内的信息导出为png图片数据
				a.download = new Date().getTime();  //设定下载名称
				a.click(); //点击触发下载
			}
		});	
		//添加测试按钮 lizhen
		// var testButton = $("action-test");
		// testButton && testButton.on("click", function(event) {
		// 	editor.getView().siblingSorting();
		// });	
		
		//遗传风险评估
		var viewriskreportButton = $('viewriskreport_button');
		var _this=this;
		viewriskreportButton && viewriskreportButton.on("click", function(event) {
            // html="<div class='mask_bg'></div>";
            // html+="<div class='pop_msg'><div class='pop_msg_hd'>消息提示</div>";
            // html+="<div class='pop_msg_bd'><p><span>正在进行遗传风险评估，请稍后......</span></p></div></div>";
            // jQuery("body").append(html);
			// editor.getView().unmarkAll();
			// /**edit by lizhen @2018-08-08**/
			// editor.getSaveLoadEngine().saveSilence(function () {
			// 	jQuery.getJSON(evaluation_url,function (res) {
			// 		jQuery.each(res,function (index, item) {
			// 			person=_this.getNode(item.tree_id);
			// 			person.setProbability(item.probability);
            //         });
            //         jQuery(".mask_bg,.pop_msg").fadeOut(500);
            //         window.setTimeout(function () {
            //             jQuery(".mask_bg,.pop_msg").detach();
            //             editor.getSaveLoadEngine().saveSilence(function () {
            //                 location.href = report_url;
            //             });
            //         },510);
            //     })
			// });
			var a = document.createElement("a");
			a.setAttribute("target", "_blank");
			a.setAttribute("href", "/riskAssess?familyTreeId=" + parent.$("#familyTreeId").val()+"&drawState=edit&ediType=1");
			document.body.appendChild(a);
			a.click();
			document.body.removeChild(a);
		});
		//概率计算
        var _this=this;
        jQuery('#action-syndrome-rate').on("click", function(event) {
        	html="<div class='mask_bg'></div>";
        	html+="<div class='pop_msg'><div class='pop_msg_hd'>消息提示</div>";
        	html+="<div class='pop_msg_bd'><p><span>正在计算携带突变概率，请稍后......</span></p></div></div>";
        	jQuery("body").append(html);
            editor.getView().unmarkAll();
            editor.getSaveLoadEngine().saveSilence(function () {
                jQuery.getJSON(evaluation_url,function (res) {
                    jQuery.each(res,function (index, item) {
                        person=_this.getNode(item.tree_id);
                        person.setProbability(item.probability);
                        person.setSyndromeRate(item.probability);
                    });
                    jQuery(".mask_bg,.pop_msg").fadeOut(500);
                    window.setTimeout(function () {
                        jQuery(".mask_bg,.pop_msg").detach();
                    },510);
                })
            });
        });
	},
		getExternalEndpoint: function() {
            return this._externalEndpointManager;
        },
        
        
        
	/**
	 * @method getNode
	 * @param {Number} nodeID
	 * @return {AbstractNode}
	 */
	getNode: function(nodeID) {
		return this.getView().getNode(nodeID);
	},

	/**
	 * @method getView
	 * @return {View}
	 */
	getView: function() {
		return this._view;
	},

	/**
	 * @method getVersionUpdater
	 * @return {VersionUpdater}
	 */
	getVersionUpdater: function() {
		return this._versionUpdater;
	},

	/**
	 * @method getGraph
	 * @return {DynamicPositionedGraph}
	 */
	getGraph: function() {
		return this._graphModel;
	},

	/**
	 * @method getController
	 * @return {Controller}
	 */
	getController: function() {
		return this._controller;
	},

	/**
	 * @method getActionStack
	 * @return {ActionStack}
	 */
	getActionStack: function() {
		return this._actionStack;
	},

	/**
	 * @method getOkCancelDialogue
	 * @return {OkCancelDialogue}
	 */
	getOkCancelDialogue: function() {
		return this._okCancelDialogue;
	},

	/**
	 * @method getNodetypeSelectionBubble
	 * @return {NodetypeSelectionBubble}
	 */
	getNodetypeSelectionBubble: function() {
		return this._nodetypeSelectionBubble;
	},

	/**
	 * @method getSiblingSelectionBubble
	 * @return {NodetypeSelectionBubble}
	 */
	getSiblingSelectionBubble: function() {
		return this._siblingSelectionBubble;
	},

	/**
	 * @method getWorkspace
	 * @return {Workspace}
	 */
	getWorkspace: function() {
		return this._workspace;
	},

	/**
	 * @method getDisorderLegend
	 * @return {Legend}
	 */
	getDisorderLegend: function() {
		return this._disorderLegend;
	},
	
	/**
	 * @method getHPOLegend
	 * @return {Legend}
	 */
	getHPOLegend: function() {
		return this._hpoLegend;
	},

	/**
	 * @method getGeneLegend
	 * @return {Legend}
	 */
	getGeneLegend: function() {
		return this._geneLegend;
	},

	/**
	 * @method getPaper
	 * @return {Workspace.paper}
	 */
	getPaper: function() {
		return this.getWorkspace().getPaper();
	},

	/**
	 * @method isReadOnlyMode
	 * @return {Boolean}
	 */
	isReadOnlyMode: function() {
		if(this.isUnsupportedBrowser()) return true;
		return false;
	},

	isUnsupportedBrowser: function() {
		if(!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1")) {
			return true;
		}
		if(!window.JSON) {
			alert("浏览器不支持");
			window.stop && window.stop();
			return true;
		}
		return false;
	},

	/**
	 * @method getSaveLoadEngine
	 * @return {SaveLoadEngine} Engine
	 */
	getSaveLoadEngine: function() {
		return this._saveLoadEngine;
	},
	//===
getFamilyData: function() {
            return this._familyData;
        },
        isFamilyPage: function() {
            return this.getFamilyData().isFamilyPage();
        },
        getFamilyMembersBeforeChanges: function() {
            return this.getFamilyData().getAllFamilyMembersList();
        },
        getFamilyMembersBeforeChanges: function() {
            return this.getFamilyData().getAllFamilyMembersList();
        },
        isFamilyMember: function(patientID) {
            return this.getFamilyData().isFamilyMember(patientID);
        },
        getPatientAccessPermissions: function(patientID) {
            var permissions = (this.getFamilyData().isFamilyMember(patientID))
                              ? this.getFamilyData().getPatientAccessPermissions(patientID) : null;
            if (permissions == null) {
                permissions = { "hasEdit": true, "hasView": true };
            }
            return permissions;
        },
        getPrintDialog: function() {
            return this._printDialog;
         },
	/**
	 * @method getProbandDataFromPhenotips
	 * @return {firstName: "...", lastName: "..."}
	 */
	getProbandDataFromPhenotips: function() {
		return this._probandData.probandData;
	},

	/**
	 * @method getTemplateSelector
	 * @return {TemplateSelector}
	 */
	getTemplateSelector: function() {
		return this._templateSelector
	},

	/**
	 * @method getImportSelector
	 * @return {ImportSelector}
	 */
	getImportSelector: function() {
		return this._importSelector
	},

	/**
	 * @method getExportSelector
	 * @return {ExportSelector}
	 */
	getExportSelector: function() {
		return this._exportSelector
	},

	/**
	 *
	 * @method isAnyMenuVisible
	 */
	isAnyMenuVisible: function() {
		if(this.getNodeMenu().isVisible() || this.getNodeGroupMenu().isVisible() || this.getPartnershipMenu().isVisible()) {
			return;
		}
	},

	/**
	 *
	 * @method generateNodeMenu
	 * @return {NodeMenu}
	 */
	generateNodeMenu: function() {
		if(this.isReadOnlyMode()) return null;
		var _this = this;
		return new NodeMenu([{
				'name': 'identifier',
				'label': '',
				'type': 'hidden',
				'tab': '个人资料'
			},
			{
				'name': 'familyUserId',
				'label': '',
				'type': 'hidden',
				'tab': '个人资料'
			},
			{
				'name': 'external_id',
				'label': '家系ID',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setExternalID'
			},
			{
				'name': 'date_of_add',
				'label': '创建日期',
				'type': 'text',
				'tab': '个人资料',
				'format': 'dd/MM/yyyy',
				'function': 'setAddDate'
			},
			{
				'name': 'first_name',
				'label': '真实姓名',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setFirstName'
			},
			{
				'name': 'tel_num',
				'label': '手机号码',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setTelephone'
			},
			{
				'name': 'gender',
				'label': '性别',
				'type': 'radio',
				'tab': '个人资料',
				'columns': 3,
				'values': [{
						'actual': 'M',
						'displayed': '男'
					},
					{
						'actual': 'F',
						'displayed': '女'
					},
					{
						'actual': 'U',
						'displayed': '未知'
					}
				],
				'default': 'M',
				'function': 'setGender'
			},
			{
				'name': 'born_addr',
				'label': '出生地',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setBornAddr'
			},
			{
				'name': 'ethnicityname',
				'label': '民族',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setEthnicityName'
			},
			{
				'name': 'curent_age',
				'label': '当前年龄',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setCurentAge'
			},
			{
				'name': 'death_age',
				'label': '死亡年龄',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setDeathAge'
			},
			{
				'name': 'deceasedText',
				'type': 'text',
				'tip': '死亡原因',
				'rows': 2,
				'tab': '个人资料',
				'function': 'setDeceasedReason'
			},
			{
				'name': 'gestation_age',
				'label': '胎龄',
				'type': 'select',
				'tab': '个人资料',
				'range': {
					'start': 0,
					'end': 45,
					'item': ['周', '周']
				},
				'nullValue': true,
				'function': 'setGestationAge'
			},
			{
				'name': 'precursor',
				'label': '设为先证者',
				'type': 'checkbox',
				'tab': '个人资料',
				'function': 'setProband'
			},
			{
				'name': 'state',
				'label': '个体状态',
				'type': 'radio',
				'columns': 2,
				'tab': '个人资料',
				'values': [{
						'actual': 'alive',
						'displayed': '健在'
					},
					{
						'actual': 'deceased',
						'displayed': '死亡<br>'
					},
					{
						'actual': 'unborn',
						'displayed': '怀孕'
					},
					{
						'actual': 'miscarriage',
						'displayed': '流产'
					},
					{
						'actual': 'stillborn',
						'displayed': '死胎'
					},
					{
						'actual': 'aborted',
						'displayed': '终止妊娠'
					}
				],
				'default': 'alive',
				'function': 'setLifeStatus'
			},
			{
				'name': 'adopted',
				'label': '领养',
				'type': 'checkbox',
				'tab': '个人资料',
				'function': 'setAdopted'
			},

			{
				'label': '子女情况',
				'name': 'childlessSelect',
				'values': [{
					'actual': 'none',
					'displayed': '不填'
				}, {
					'actual': 'childless',
					'displayed': '无子女'
				}, {
					'actual': 'infertile',
					'displayed': '不孕不育'
				}],
				'type': 'select',
				'tab': '个人资料',
				'function': 'setChildlessStatus'
			},
			{
				'name': 'monozygotic',
				'label': '同卵双生',
				'type': 'checkbox',
				'tab': '个人资料',
				'function': 'setMonozygotic'
			},
			{
				'name': 'unknownzygotic',
				'label': '单双卵未知双生',
				'type': 'checkbox',
				'tab': '个人资料',
				'function': 'setUnknownzygotic'
			},
			{
				'name': 'placeholder',
				'label': '占位符节点',
				'type': 'checkbox',
				'tab': '个人资料',
				'function': 'makePlaceholder'
			},
			{
				'name': 'carrier',
				'label': ' ',
				'type': 'radio',
				'tab': '个人资料',
				'columns': 3,
				'values': [{
						'actual': '',
						'displayed': '未受影响者'
					},
					{
						'actual': 'carrier',
						'displayed': '肯定携带者'
					},
					{
						'actual': 'affected',
						'displayed': '受累者'
					}
				],
				'default': '',
				'function': 'setCarrierStatus'
			},
			
			{
				'name': 'evaluated',
				'label': '结果评估',
				'type': 'checkbox',
				'tab': '个人资料',
				'function': 'setEvaluated'
			},
			{
				'name': 'evaluatedinfo',
				'type': 'text',
				'tip': '结果描述',
				'tab': '个人资料',
				'function': 'setEvaluatedInfo'
			},
			{
				'name': 'probability',
				'label': '携带致病基因概率(%)',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setProbability'
			},
			{
				'name': 'comments',
				'label': '备注',
				'type': 'text',
				'tab': '个人资料',
				'function': 'setComments'
			},
			{
				'name': 'canceragetype',
				'label': '年龄类型',
				'type': 'select',
				'tab': '添加癌症',
				'columns': 2,
				'values': [{
						'actual': 'none',
						'displayed': '未知（U）'
					},
					{
						'actual': 'estimatedage',
						'displayed': '估计（A）'
					},
					{
						'actual': 'exactage',
						'displayed': '确切（E）'
					}
				],
				'nullValue': true,
				'function': 'setCancerAgeType'
			},
			{
				'name': 'canceragenum',
				'type': 'text',
				'dependency': 'canceragetype != none',
				'tip': '患癌年龄',
				'tab': '添加癌症',
				'function': 'setCancerAgeNum'
			},
			{
				'name': 'setcancertypes',
				'label': '选择癌种',
				'type': 'select',
				'tab': '添加癌症',
				'columns': 2,
				'values': [{
						'actual': 'cancertype1',
						'displayed': '高相关癌<br>'
					},
					{
						'actual': 'cancertype2',
						'displayed': '低相关-泌尿生殖系肿瘤<br>'
					},
					{
						'actual': 'cancertype3',
						'displayed': '低相关-胃肠道肿瘤<br>'
					},
					{
						'actual': 'cancertype4',
						'displayed': '低相关-其他肿瘤<br>'
					},
					{
						'actual': 'cancertype5',
						'displayed': '低相关-罕见肿瘤<br>'
					}
				],
				'nullValue': true,
				'function': 'setCancerTypes'
			},
			{
				'name': 'high_relative_cancer',
				'label': '选择高相关癌症名',
				'type': 'select',
				'tab': '添加癌症',
				'values': [{
						'actual': 'highcancer_rxa',
						'displayed': '乳腺癌'
					},
					{
						'actual': 'highcancer_lca',
						'displayed': '卵巢癌'
					},
					{
						'actual': 'highcancer_zgnma',
						'displayed': '子宫内膜癌'
					},
					{
						'actual': 'highcancer_jzca',
						'displayed': '结直肠癌'
					},
					{
						'actual': 'highcancer_wa',
						'displayed': '胃癌'
					},
					
					{
						'actual': 'highcancer_qlxa',
						'displayed': '前列腺癌'
					},
					{
						'actual': 'highcancer_sa',
						'displayed': '肾癌'
					},
					{
						'actual': 'highcancer_jzxa',
						'displayed': '甲状腺癌'
					},
					{
						'actual': 'highcancer_yxa',
						'displayed': '胰腺癌'
					}
				],
				'nullValue': true,
				'function': 'setHighRelativeCancerName'
			},

			{
				'name': 'urogenital_tumor',
				'label': '低相关-泌尿生殖系肿瘤名',
				'type': 'select',
				'tab': '添加癌症',
				'values': [{
						'actual': '卵巢纤维肉瘤',
						'displayed': '卵巢纤维肉瘤'
					},
					{
						'actual': '卵巢伴环状小管性索瘤',
						'displayed': '卵巢伴环状小管性索瘤'
					},
					{
						'actual': '高钙血型小细胞卵巢癌',
						'displayed': '高钙血型小细胞卵巢癌'
					},
					{
						'actual': '输卵管癌',
						'displayed': '输卵管癌'
					},
					{
						'actual': '肾上腺皮质癌',
						'displayed': '肾上腺皮质癌'
					},
					{
						'actual': '肾血管平滑肌脂肪瘤',
						'displayed': '肾血管平滑肌脂肪瘤'
					},
					{
						'actual': '膀胱癌',
						'displayed': '膀胱癌'
					},
					{
						'actual': '胆管癌',
						'displayed': '胆管癌'
					},
					{
						'actual': '输尿管癌',
						'displayed': '输尿管癌'
					},
					{
						'actual': '肾盂肾癌',
						'displayed': '肾盂肾癌'
					},
					{
						'actual': '尿路癌',
						'displayed': '尿路癌'
					},
					{
						'actual': '睾丸癌',
						'displayed': '睾丸癌'
					},
					{
						'actual': '睾丸支持细胞瘤',
						'displayed': '睾丸支持细胞瘤'
					},
					{
						'actual': '睾丸大细胞钙化型支持细胞瘤',
						'displayed': '睾丸大细胞钙化型支持细胞瘤'
					},
					{
						'actual': '宫颈癌',
						'displayed': '宫颈癌'
					},
					{
						'actual': '宫颈恶性腺瘤',
						'displayed': '宫颈恶性腺瘤'
					}
				],
				'nullValue': true,
				'function': 'setUrogenitalTumorName'
			},
			{
				'name': 'gastrointestinal_tumor',
				'label': '低相关-胃肠道肿瘤名',
				'type': 'select',
				'tab': '添加癌症',
				'values': [{
						'actual': '胃肠间质瘤',
						'displayed': '胃肠间质瘤'
					},
					{
						'actual': '胃肠道错构瘤',
						'displayed': '胃肠道错构瘤'
					},
					{
						'actual': '胃肠道弥漫性神经节瘤病',
						'displayed': '胃肠道弥漫性神经节瘤病'
					},
					{
						'actual': '食管癌',
						'displayed': '食管癌'
					},
					{
						'actual': '肝癌',
						'displayed': '肝癌'
					},
					{
						'actual': '胆囊癌',
						'displayed': '胆囊癌'
					},
					{
						'actual': '小肠癌',
						'displayed': '小肠癌'
					}
				],
				'nullValue': true,
				'function': 'setGastrointestinalTumorName'
			},
			{
				'name': 'other_tumor',
				'label': '低相关-其他肿瘤名',
				'type': 'select',
				'tab': '添加癌症',
				'values': [{
						'actual': '肺癌',
						'displayed': '肺癌'
					},
					{
						'actual': '脑瘤',
						'displayed': '脑瘤'
					},
					{
						'actual': '神经胶质瘤',
						'displayed': '神经胶质瘤'
					},
					{
						'actual': '白血病',
						'displayed': '白血病'
					},
					{
						'actual': '霍奇金淋巴瘤',
						'displayed': '霍奇金淋巴瘤'
					},
					{
						'actual': '非霍奇金淋巴瘤',
						'displayed': '非霍奇金淋巴瘤'
					},
					{
						'actual': '黑色素瘤',
						'displayed': '黑色素瘤'
					},
					{
						'actual': '皮肤癌',
						'displayed': '皮肤癌'
					},
					{
						'actual': '鼻咽癌',
						'displayed': '鼻咽癌'
					},
					{
						'actual': '喉头癌',
						'displayed': '喉头癌'
					},
					{
						'actual': '舌癌',
						'displayed': '舌癌'
					},
					{
						'actual': '骨癌',
						'displayed': '骨癌'
					},
					{
						'actual': '原发性腹膜癌',
						'displayed': '原发性腹膜癌'
					}
				],
				'nullValue': true,
				'function': 'setOtherTumorName'
			},
			{
				'name': 'rare_tumor',
				'label': '低相关-罕见肿瘤名',
				'type': 'select',
				'tab': '添加癌症',
				'values': [{
						'actual': '儿童肉瘤',
						'displayed': '儿童肉瘤'
					},
					{
						'actual': '骨肉瘤',
						'displayed': '骨肉瘤'
					},
					{
						'actual': '软组织肉瘤',
						'displayed': '软组织肉瘤'
					},
					{
						'actual': '神经节瘤',
						'displayed': '神经节瘤'
					},
					{
						'actual': '副神经节瘤',
						'displayed': '副神经节瘤'
					},
					{
						'actual': '嗜铬细胞瘤',
						'displayed': '嗜铬细胞瘤'
					},
					{
						'actual': '心脏横纹肌瘤',
						'displayed': '心脏横纹肌瘤'
					},
					{
						'actual': '横纹肌肉瘤',
						'displayed': '横纹肌肉瘤'
					},
					{
						'actual': '脉络丛癌',
						'displayed': '脉络丛癌'
					},
					{
						'actual': '砂粒性黑色素神经鞘瘤',
						'displayed': '砂粒性黑色素神经鞘瘤'
					},
					{
						'actual': '淋巴管肌瘤病',
						'displayed': '淋巴管肌瘤病'
					},
					{
						'actual': '内淋巴囊肿瘤',
						'displayed': '内淋巴囊肿瘤'
					},
					{
						'actual': '巨细胞星形细胞瘤',
						'displayed': '巨细胞星形细胞瘤'
					},
					{
						'actual': '皮肤和黏膜粘液瘤',
						'displayed': '皮肤和黏膜粘液瘤'
					},
					{
						'actual': '心肌粘液瘤',
						'displayed': '心肌粘液瘤'
					},
					{
						'actual': '乳腺粘液瘤',
						'displayed': '乳腺粘液瘤'
					},
					{
						'actual': '骨软骨粘液瘤',
						'displayed': '骨软骨粘液瘤'
					},
					{
						'actual': '神经纤维瘤',
						'displayed': '神经纤维瘤'
					},
					{
						'actual': '丛状神经纤维瘤',
						'displayed': '丛状神经纤维瘤'
					},
					{
						'actual': '面部血管纤维瘤',
						'displayed': '面部血管纤维瘤'
					},
					{
						'actual': '甲周纤维瘤',
						'displayed': '甲周纤维瘤'
					},
					{
						'actual': '牙龈纤维瘤',
						'displayed': '牙龈纤维瘤'
					},
					{
						'actual': '视网膜结节状错构瘤',
						'displayed': '视网膜结节状错构瘤'
					},
					{
						'actual': '视网膜毛细血管瘤',
						'displayed': '视网膜毛细血管瘤'
					},
					{
						'actual': '视网膜母细胞瘤',
						'displayed': '视网膜母细胞瘤'
					},
					{
						'actual': '恶性胶质瘤',
						'displayed': '恶性胶质瘤'
					},
					{
						'actual': '视神经胶质瘤',
						'displayed': '视神经胶质瘤'
					},
					{
						'actual': '毛鞘瘤',
						'displayed': '毛鞘瘤'
					},
					{
						'actual': '皮肤黏膜神经瘤',
						'displayed': '皮肤黏膜神经瘤'
					},
					{
						'actual': '眼部神经瘤',
						'displayed': '眼部神经瘤 '
					},
					{
						'actual': '口腔神经瘤',
						'displayed': '口腔神经瘤 '
					},
					{
						'actual': '口腔乳头瘤',
						'displayed': '口腔乳头瘤'
					},
					{
						'actual': '皮脂腺瘤',
						'displayed': '皮脂腺瘤'
					},
					{
						'actual': '角化棘皮瘤',
						'displayed': '角化棘皮瘤'
					},
					{
						'actual': '细支气管肺泡癌',
						'displayed': '细支气管肺泡癌'
					},
					{
						'actual': '硬纤维瘤',
						'displayed': '硬纤维瘤'
					},
					{
						'actual': '脂肪瘤',
						'displayed': '脂肪瘤'
					},
					{
						'actual': '甲状腺腺瘤',
						'displayed': '甲状腺腺瘤'
					},
					{
						'actual': '乳腺导管腺瘤',
						'displayed': '乳腺导管腺瘤'
					},
					{
						'actual': '中枢神经系统血管膜细胞瘤',
						'displayed': '中枢神经系统血管膜细胞瘤'
					},
					{
						'actual':'肝母细胞瘤',
						'displayed':'肝母细胞瘤'
					}
				],
				'nullValue': true,
				'function': 'setRareTumorName'
			},
			{
				'name': 'site_of_cancer',
				'label': '部位',
				'type': 'text',
				'tip': '部位',
				'tab': '添加癌症',
				'function': 'setSiteOfCancerValue'
			},
			{
				'name': 'cancer_is_ok',
				'label':'',
				'value': '确定',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setCancerSite'
			},
			{
				'name': 'genetic_syndrome_rxa',
				'label': '',
				'value': '乳腺癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticRxaSyndrome'
			},
			{
				'name': 'genetic_syndrome_lca',
				'label': '',
				'value': '卵巢癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticLcaSyndrome'
			},
			{
				'name': 'genetic_syndrome_zgnma',
				'label': '',
				'value': '子宫内膜癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticZgnmaSyndrome'
			},
			{
				'name': 'genetic_syndrome_jzca',
				'label': '',
				'value': '结直肠癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticJzcaSyndrome'
			},
			{
				'name': 'genetic_syndrome_wa',
				'label': '',
				'value': '胃癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticWaSyndrome'
			},
			{
				'name': 'genetic_syndrome_qlxa',
				'label': '',
				'value': '前列腺癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticQlxaSyndrome'
			},
			{
				'name': 'genetic_syndrome_sa',
				'label': '',
				'value': '肾癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticSaSyndrome'
			},
			{
				'name': 'genetic_syndrome_jzxa',
				'label': '',
				'value': '甲状腺癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticJzxaSyndrome'
			},
			{
				'name': 'genetic_syndrome_yxa',
				'label': '',
				'value': '胰腺癌辅助信息',
				'type': 'button',
				'tab': '添加癌症',
				'function': 'setGeneticYxaSyndrome'
			},
			{
				'name': 'genetic_syndrome',
				'label': '',
				'value': '添加基本健康信息',
				'type': 'button',
				'tab': '添加基本健康信息',
				'function': 'setGeneticSyndrome'
			},
			/*
			{
				'name': 'genetic_file',
				'label': '',
				'value': '检测结果上传',
				'type': 'button',
				'tab': '癌前病变及检测结果上传',
				'function': 'setGeneticFile'
			},
			*/
            {
                'name': 'syndrome_rate',
                'label': '',
                'type': 'div',
                'tab': '携带突变概率',
				'function':'setSyndromeRate',
            },
			{
			     'name' : 'disorders',
			     'label' : '癌症病史',
			     'type' : 'disease-picker',
			     'tab': '添加癌症',			              
				 'function' : 'setDisorders'
			},
		], ["个人资料", "添加癌症", "添加基本健康信息","携带突变概率"]);
	},

	/**
	 * @method getNodeMenu
	 * @return {NodeMenu}
	 */
	getNodeMenu: function() {
		return this._nodeMenu;
	},

	/**
	 *
	 * @method generateNodeGroupMenu
	 * @return {NodeMenu}
	 */
	generateNodeGroupMenu: function() {
		if(this.isReadOnlyMode()) return null;
		var _this = this;
		return new NodeMenu([{
				'name': 'external_ids',
				'label': '家系ID',
				'type': 'text',
				'function': 'setExternalID'
			},
			{
				'name': 'identifier',
				'label': '',
				'type': 'hidden'
			},
			{
				'name': 'gender',
				'label': '性别',
				'type': 'radio',
				'columns': 3,
				'values': [{
						'actual': 'M',
						'displayed': '男'
					},
					{
						'actual': 'F',
						'displayed': '女'
					},
					{
						'actual': 'U',
						'displayed': '未知'
					}
				],
				'default': 'U',
				'function': 'setGender'
			},
			{
				'name': 'numInGroup',
				'label': '包含人数',
				'type': 'select',
				'values': [{
						'actual': 1,
						displayed: 'N'
					}, {
						'actual': 2,
						displayed: '2'
					}, {
						'actual': 3,
						displayed: '3'
					},
					{
						'actual': 4,
						displayed: '4'
					}, {
						'actual': 5,
						displayed: '5'
					}, {
						'actual': 6,
						displayed: '6'
					},
					{
						'actual': 7,
						displayed: '7'
					}, {
						'actual': 8,
						displayed: '8'
					}, {
						'actual': 9,
						displayed: '9'
					}
				],
				'function': 'setNumPersons'
			},

			{
				'name': 'comments',
				'label': '备注',
				'type': 'textarea',
				'rows': 2,
				'function': 'setComments'
			}
		], []);
	},

	/**
	 * @method getNodeGroupMenu
	 * @return {NodeMenu}
	 */
	getNodeGroupMenu: function() {
		return this._nodeGroupMenu;
	},

	/**
	 *
	 * @method generatePartnershipMenu
	 * @return {NodeMenu}
	 */
	generatePartnershipMenu: function() {
		if(this.isReadOnlyMode()) return null;
		var _this = this;
		return new NodeMenu([
			{
				'name': 'consangr',
				'label': '婚配类型',
				'type': 'radio',
				'columns': 1,
				'values': [
					{
						'actual': 'N',
						'displayed': '非近亲婚配<br>'
					},
					{
						'actual': 'E',
						'displayed': '婚外恋<br>'
					},
					{
						'actual': 'Y',
						'displayed': '近亲婚配<br>'
					}
				],
				'default': 'N',
				'function': 'setConsanguinity'
			},
			{
				'name': 'broken',
				'label': '离婚',
				'type': 'checkbox',
				'dependency': 'consangr != E',
				'function': 'setBrokenStatus'
			}
		], [], "relationship-menu");
	},

	/**
	 * @method getPartnershipMenu
	 * @return {NodeMenu}
	 */
	getPartnershipMenu: function() {
		return this._partnershipMenu;
	},

	/**
	 * @method convertGraphCoordToCanvasCoord
	 * @return [x,y]
	 */
	convertGraphCoordToCanvasCoord: function(x, y) {
		var scale = PedigreeEditor.attributes.layoutScale;
		return {
			x: x * scale.xscale,
			y: y * scale.yscale
		};
	},

	/**
	 *
	 * @method initializeSave
	 */
	startAutoSave: function(intervalInSeconds) {
		setInterval(function() {
			editor.getSaveLoadEngine().save()
		}, intervalInSeconds * 1000);
	}
});

var editor;

PedigreeEditor.attributes = {
	propagateLastName: true,
	radius: 40,
	orbRadius: 12,
	touchOrbRadius: 12,
	personHoverBoxRadius: 90,
	newHandles: true,
	personHandleLength: 75,
	personHandleBreakX: 55,
	personHandleBreakY: 53,
	personSiblingHandleLengthX: 65,
	personSiblingHandleLengthY: 30,
	enableHandleHintImages: false, 
	handleStrokeWidth: 0, 
	handleDragStrokeWidth: 0, 
	/*groupNodesScale: 0.85 家系图中集合大小修改*/
	groupNodesScale: 1,

	childlessLength: 14,
	infertileMarkerHeight: 4,
	infertileMarkerWidth: 14,
	twinCommonVerticalLength: 6,
	twinMonozygothicLineShiftY: 24,
	curvedLinesCornerRadius: 25,
	unbornShape: {
		'font-size': 40,
		'font-family': 'Cambria'
	},
	carrierShape: {
		fill: '#000'
	},
	carrierDotRadius: 12,
	presymptomaticShape: {
		fill: '#f5f5f5',
		"stroke": "#555555"
	},
	presymptomaticShapeWidth: 8,
	evaluationShape: {
		'font-size': 40,
		'font-family': 'Arial'
	},
	nodeShape: {
		fill: "#f5f5f5",
		stroke: "#333333",
		//'stroke-width': "0.15rem", chen.yuguo 圆圈变大
		'stroke-width':'0.20rem',
		"fill-opacity": 1
	}, // this
	nodeShapeMenuOn: {
		fill: "#000",
		stroke: "none",
		"fill-opacity": 0.3
	},
	nodeShapeMenuOff: {
		fill: "#000",
		stroke: "none",
		"fill-opacity": 0.1
	},
	nodeShapeMenuOnPartner: {
		fill: "#595959",
		stroke: "none",
		"fill-opacity": 0.6
	},
	nodeShapeMenuOffPartner: {
		fill: "#595959",
		stroke: "none",
		"fill-opacity": 0
	},
	nodeShapeDiag: {
		fill: "#f5f5f5",
		//stroke: "#595959"
		//chen.yugo 未知性别大小和颜色改变下面都是
		//stroke: "#595959"
		stroke: "#333",
		'stroke-width':'0.20rem'
	}, // this
	boxOnHover: {
		fill: "#48ad47",
		stroke: "none",
		opacity: 0.3,
		"fill-opacity": 0.1
	}, 
	menuBtnIcon: {
		fill: "#1F1F1F",
		stroke: "none"
	},
	deleteBtnIcon: {
		fill: "#48ad47",
		stroke: "none",
		width: "36px",
		height: "36px",
	},
	btnMaskHoverOn: {
		opacity: .6,
		stroke: 'none'
	},
	btnMaskHoverOff: {
		opacity: 0
	},
	btnMaskClick: {
		opacity: 1
	},
	orbHue: 0.5,
	phShape: {
		fill: "white",
		"fill-opacity": 0,
		"stroke": 'black',
		"stroke-dasharray": "- "
	},
	dragMeLabel: {
		'font-size': 14,
		'font-family': 'Tahoma'
	},
	pedNumberLabel: {
		'font-size': 19,
		'font-family': 'Serif'
	},
	descendantGroupLabel: {
		'font-size': 21,
		'font-family': 'Tahoma'
	},
	label: {
		'font-size': 20,//chen.yuguo
		'font-family': 'Arial'
	},
	nameLabels: {
		'font-size': 20,
		'font-family': 'Arial'
	},
	commentLabel: {
		'font-size': 19,
		'font-family': 'Arial'
	},
	evaluatedInfoLabel: {
		'font-size': 19,
		'font-family': 'Arial'
	},
	externalIDLabels: {
		'font-size': 18,
		'font-family': 'Arial'
	},
	probabilityLabels: {
		'font-size': 18,
		'font-family': 'Arial',
	},
	disorderShapes: {},
	partnershipNode: {
		fill: '#48ad47',
		stroke: '#48ad47',
		'stroke-width': 4
	}, 
	partnershipRadius: 9.5,
	partnershipHandleBreakY: 15,
	partnershipHandleLength: 36,
	partnershipLines: {
		"stroke-width": 1.25,
		stroke: '#303058'
	},
	consangrPartnershipLines: {
		"stroke-width": 1.25,
		stroke: '#DC143C'
	},
	consangrPartnershipDotLines: {
		"stroke-width": 4,
		stroke: '#32CD32',
		"stroke-dasharray": "-"
	},
	noContactLines: {
		"stroke-width": 12.75,
		stroke: '#5547ad',
		"stroke-dasharray": "."
	},
	adoptedPartnershipDotLines: {
		"stroke-width": 4,
		stroke: '#32CD32',
		"stroke-dasharray": "-"
	},
	unknownzygoticText: {
		'font-size': 28,
		'font-family': 'Arial'
	},
	unknownzygoticLine: {
		"stroke-width": 1,
		stroke: '#32CD32'
	},
	notInContactLineSize: 120,
	graphToCanvasScale: 8,
	layoutRelativePersonWidth: 10,
	layoutRelativeOtherWidth: 2,
	layoutScale: {
		xscale: 14.0,
		yscale: 15.0,//10.0 chen.yuguo
	}
};
document.observe("xwiki:dom:loaded", function() {
	editor = new PedigreeEditor();
});